using System;
using System.Collections.Generic;
using System.Web.UI;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class BreadCrumbler : UserControl
    {
        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>The path.</value>
        public string Path
        {
            get { return Request["Path"]; }
        }

        public string Root
        {
            get { return SvnWebDav.CurrentRepositoryRootUrl.TrimEnd('/'); }
        }

        #region Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && Visible)
            {
                BreadCrumbs breadCrumbs = new BreadCrumbs();
                breadCrumbs.Add(new BreadCrumb(Root, GetUrl("/"), true));

                string path = string.Empty;
                foreach (string crumb in Path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    path += string.Format("/{0}", crumb);
                    breadCrumbs.Add(new BreadCrumb(crumb, GetUrl(path), true));
                }
                breadCrumbs[breadCrumbs.Count - 1].IsLink = false;

                BreadCrumbRepeater.DataSource = breadCrumbs;
                BreadCrumbRepeater.DataBind();
            }
        }

        /// <summary>
        /// Gets the URL.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        private string GetUrl(string path)
        {
            return (!string.IsNullOrEmpty(Request["RevisionId"]) && !IsFolder(path))
                ? string.Format("{0}?RevisionId={1}&Path={2}", Request.FilePath, Request["RevisionId"], path)
                : string.Format("{0}?Path={1}", Request.FilePath, path);
        }

        /// <summary>
        /// Determines whether the specified path is folder.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// 	<c>true</c> if the specified path is folder; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsFolder(string path)
        {
            return SvnWebDav.IsFolder(path);
        }

        #endregion

        #region Internal Classes

        /// <summary>
        /// Represents a bread crumb link
        /// </summary>
        internal class BreadCrumb
        {
            private string text;
            private string navigateUrl;
            private bool isLink;

            /// <summary>
            /// Gets or sets the text.
            /// </summary>
            /// <value>The text.</value>
            public string Text
            {
                get { return text; }
                set { text = value; }
            }

            /// <summary>
            /// Gets or sets the navigate URL.
            /// </summary>
            /// <value>The navigate URL.</value>
            public string NavigateUrl
            {
                get { return navigateUrl; }
                set { navigateUrl = value; }
            }

            /// <summary>
            /// Gets or sets a value indicating whether this instance is link.
            /// </summary>
            /// <value><c>true</c> if this instance is link; otherwise, <c>false</c>.</value>
            public bool IsLink
            {
                get { return isLink; }
                set { isLink = value; }
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="BreadCrumb"/> class.
            /// </summary>
            /// <param name="text">The text.</param>
            /// <param name="navigateUrl">The navigate URL.</param>
            /// <param name="isLink">if set to <c>true</c> [is link].</param>
            public BreadCrumb(string text, string navigateUrl, bool isLink)
            {
                this.text = text;
                this.navigateUrl = navigateUrl;
                this.isLink = isLink;
            }
        }

        /// <summary>
        /// Represents a list of <see cref="BreadCrumb"/>s
        /// </summary>
        internal class BreadCrumbs : List<BreadCrumb> {}

        #endregion
    }
}